﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public interface UIDraw
{
	void Draw();
}

public class RoomAvailable: UIDraw
{
	Texture2D icon;
	string roomName;
	int numberOfPlayers;
	bool isRoomFull;
	
	static Texture2D roomBG;
	static GUIStyle style;
	static Texture2D fullRoom;
	static Texture2D emptyRoom;
	
	static GUIStyle medianStyle;
	
	public GameObject gameObject;
	
	public static void setDefaults(Texture2D RoomBg, Texture2D FullRoom, Texture2D EmptyRoom)
	{
		style = new GUIStyle();
		roomBG = RoomBg;
		fullRoom = FullRoom;
		emptyRoom = EmptyRoom;
		medianStyle = new GUIStyle();
		medianStyle.border = new RectOffset(5,5,5,5);
	}
	
	public RoomAvailable(Texture2D icon, string roomName)
	{
		isRoomFull = false;
		this.icon = icon;
		this.roomName = roomName;
		this.numberOfPlayers = 1;
		
	}
	
	public void Draw ()
	{
		GUILayout.BeginHorizontal(/*GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false)*/);
		GUILayout.BeginHorizontal(roomBG, style/*,GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false)*/);
		
		GUILayout.Label(icon/*, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false)*/);
		GUILayout.Label(roomName);
		GUILayout.Label(numberOfPlayers + " / 2");
		
		GUILayout.EndHorizontal();
		GUILayout.Label((isRoomFull)? fullRoom: emptyRoom/*, 
								GUILayout.ExpandWidth(true), 
								GUILayout.ExpandHeight(true)*/);
		GUILayout.EndHorizontal();
	}
}

public class GameLobby : MonoBehaviour {
	
	enum UITYPE
	{
		CHAT,
		ROOM
	}
	public Texture2D layout;
	float roomVertScroll = 0;
	Vector2 roomScroll = Vector2.zero;
	Vector2 chatScroll = Vector2.zero;
	
	private Texture2D fullRoom;
	private Texture2D emptyRoom;
	private Texture2D roomName;
	private Texture2D icon;
	
	private Texture2D roomNameBG;
	private Texture2D chatBg;
	private Texture2D bg1;
	private Texture2D bg2;
	
	Dictionary<UITYPE, List<UIDraw>> uiDict;
	 
	// Use this for initialization
	
	void Awake()
	{
		layout = Resources.Load("LAYOUT") as Texture2D;
		fullRoom = Resources.Load("FULL") as Texture2D;
		emptyRoom = Resources.Load("JOIN")as Texture2D ;
		roomName = Resources.Load("ROOMNAME") as Texture2D;
		roomNameBG = Resources.Load("ROOMNAMEBG") as Texture2D;
		chatBg = Resources.Load("CHATBG") as Texture2D;
		icon = Resources.Load("FACEICON") as Texture2D;
		bg1 = Resources.Load("BG1") as Texture2D;
		bg2 = Resources.Load("BG2") as Texture2D;
	}
	
	void Start () {
		//uiDict = new Dictionary<UITYPE, UIDraw>
		uiDict = new Dictionary<UITYPE, List<UIDraw>>();
		uiDict.Add(UITYPE.CHAT, new List<UIDraw>());
		uiDict.Add(UITYPE.ROOM, new List<UIDraw>());
		RoomAvailable.setDefaults(roomName, fullRoom, emptyRoom);
		
		for(int i =0; i < 4; i++)
			uiDict[UITYPE.ROOM].Add(new RoomAvailable(icon, "Almira Room"));
	}
	
	void OnGUI()
	{
		Rect fullRect = new Rect(0,0, Screen.width, Screen.height);
		GUI.DrawTexture(fullRect, layout);
		GUI.DrawTexture(fullRect, bg1);
		
		Rect lastRect = new Rect(Screen.width * 0.1f, Screen.height * 0.175f, Screen.width * 0.55f, Screen.height * 0.60f);
	
		GUILayout.BeginArea(lastRect, roomNameBG);
		GUILayout.BeginHorizontal();
			roomScroll = GUILayout.BeginScrollView(roomScroll, false, true, GUILayout.MaxHeight(lastRect.height * 0.55f), GUILayout.ExpandWidth(false));
		
			//GUILayout.BeginArea(fullRect);
				DrawUI(UITYPE.ROOM);
			//GUILayout.EndArea();
		
			GUILayout.EndScrollView();
		GUILayout.EndHorizontal();
		GUILayout.EndArea();
		
		Rect chatRect = new Rect(Screen.width * 0.1f, Screen.height * 0.6f, Screen.width * 0.5f, Screen.height * 0.10f);
		GUILayout.BeginArea(chatRect, chatBg);
			chatScroll = GUILayout.BeginScrollView(chatScroll, false, true, GUILayout.MaxHeight(chatRect.height));
			GUILayout.EndScrollView();
		GUILayout.EndArea();
		
		GUI.DrawTexture(fullRect, bg2);
	}
	
	void DrawUI(UITYPE uiType)
	{
		int length = uiDict[uiType].Count;
		for(int i =0; i < length; i++)
			uiDict[uiType][i].Draw();
	}
	
}
